package com.zhuangjie.shop.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.coupon.dto.QueryConfirmRequireDTO;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.shop.dto.CartItemDTO;
import com.zhuangjie.shop.service.ShopService;
import com.zhuangjie.shop.service.ShoppingCartService;
import com.zhuangjie.shop.vo.CartItemVO;
import com.zhuangjie.shop.vo.order_confirm_require.OrderConfirmRequireVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 购物车控制器
 *
 * @author zhuangjie
 * @date 2023/11/20
 */
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/put")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    @ApiOperation("添加商品到购物车")
    public R addToCart(@RequestBody CartItemDTO cartItemDTO) {
        CartItemVO inRedisCartItemDTO = shoppingCartService.addToCart(cartItemDTO);
        return R.ok().data(inRedisCartItemDTO);
    }

    @GetMapping("/list/{shopId}")
    @ApiOperation("查询用户购物车")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R listUserShoppingCart(@PathVariable("shopId") Long shopId) {
        List<CartItemVO> cartItemDTOList =  shoppingCartService.listUserShoppingCart(shopId);
        return R.ok().data(cartItemDTOList);
    }

    @PostMapping("/order-confirm-require")
    @ApiOperation("查询订单确认需要的数据")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryOrderConfirmRequire(@Valid @RequestBody QueryConfirmRequireDTO queryConfirmRequireDTO) throws ExecutionException, InterruptedException {
        return R.require(OrderConfirmRequireVO.class,shopService.queryOrderConfirmRequire(queryConfirmRequireDTO));
    }

    @PostMapping("/clear/{shopId}")
    @ApiOperation("清空购物车")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R clearShoppingCart(@NotNull(message = "店铺id不能为空") @PathVariable("shopId") Long shopId) {
        return R.capture(()->shoppingCartService.clearShoppingCart(shopId));
    }



}
